package com.jian.android.autojipai.app.helper;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.util.Log;

import com.jian.android.autojipai.app.App;
import com.jian.android.autojipai.data.bean.EventBusOb;
import com.jian.android.autojipai.data.bean.PokerDetection;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.greenrobot.eventbus.EventBus;
import org.tensorflow.lite.support.image.TensorImage;
import org.tensorflow.lite.support.label.Category;
import org.tensorflow.lite.task.vision.detector.Detection;
import org.tensorflow.lite.task.vision.detector.ObjectDetector;

/* loaded from: classes.dex */
public class TensorFlowLiteTool {
    private static final String TAG = "---TensorFlowLiteTool";
    private DetectionResultCallBack detectionResultCallBack;
    private ObjectDetector detector;
    private ObjectDetector.ObjectDetectorOptions options;

    /* loaded from: classes.dex */
    public interface DetectionResultCallBack {
        void returnDetectionResult(List<PokerDetection> list, Bitmap bitmap);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static class TensorFlowLiteToolInstance {
        private static final TensorFlowLiteTool instance = new TensorFlowLiteTool();

        private TensorFlowLiteToolInstance() {
        }
    }

    private TensorFlowLiteTool() {
    }

    public void setDetectionResultCallBack(DetectionResultCallBack detectionResultCallBack) {
        this.detectionResultCallBack = detectionResultCallBack;
    }

    public static TensorFlowLiteTool getInstance() {
        return TensorFlowLiteToolInstance.instance;
    }

    public void initObjectDetector(Context context) {
        if (this.options == null) {
            this.options = ObjectDetector.ObjectDetectorOptions.builder().setMaxResults(54).setScoreThreshold(0.1f).build();
        }
        if (this.detector == null) {
            File file = new File(context.getFilesDir() + File.separator + "1.tflite");
            if (!file.exists()) {
                Log.e(TAG, "tflite不存在");
                return;
            }
            try {
                this.detector = ObjectDetector.createFromFileAndOptions(file, this.options);
            } catch (Exception e) {
                Log.e(TAG,  e.getMessage());
            }
        }
    }

    public void detector(Bitmap bitmap) {
        RectF boundingBox;
        App.isCanDetection = false;
        Log.e(TAG,  "截屏Bitmap宽高：" + bitmap.getWidth() + "," + bitmap.getHeight());
        List<Detection> detect = this.detector.detect(TensorImage.fromBitmap(bitmap));
        Log.e(TAG,  "*********************************************************************");
        Log.e(TAG,  "目标对象集合个数：" + detect.size());
        if ( detect.size() > 0) {
            List<PokerDetection> arrayList = new ArrayList();
            for (int i = 0; i < detect.size(); i++) {
                Detection detection = detect.get(i);
                if (detection.getBoundingBox().top > bitmap.getHeight() / 10) {
                     Log.e(TAG,  "对象个数：" + (i + 1));
                    boundingBox = detection.getBoundingBox();
                     Log.e(TAG,  "左上：" + boundingBox.left + "，" + boundingBox.top + " - 右下：" + boundingBox.right + "，" + boundingBox.bottom);

                     int categorieSize =detection.getCategories().size();
                   for (int j = 0; j < categorieSize; j++){
                       Log.e(TAG,  "类型个数：" + j);
                       Category category = detection.getCategories().get(j);
                       Log.e(TAG,  "标签：" + category.getLabel() + "；" + category.getDisplayName());
                       Log.e(TAG,  "置信度：" + (category.getScore() * 100.0f) + "");
                       Log.e(TAG,  "*********************************************************************");
                       arrayList.add(new PokerDetection(category.getLabel()));
                    }

                }
            }
            Log.e(TAG,  "arrayList：" +arrayList.size());
            if (arrayList.size() > 0) {
                DetectionResultCallBack detectionResultCallBack = this.detectionResultCallBack;
                if (detectionResultCallBack != null) {
                    detectionResultCallBack.returnDetectionResult(arrayList, bitmap);
                }
                if (bitmap != null && !bitmap.isRecycled()) {
                    bitmap.recycle();
                }
                PokerDataHandleTool.getInstance().handPokerData(arrayList);
                return;
            }
            App.isCanDetection = true;
            if (bitmap != null && !bitmap.isRecycled()) {
                bitmap.recycle();
            }
             Log.e(TAG,  "没有检测出类别对象，继续截取屏幕截图");
            return;
        }
        App.detectionZoneCount+=1;
        Log.e(TAG,  "detectionZoneCount：" + App.detectionZoneCount);
        if (App.detectionZoneCount >=5){
            EventBus.getDefault().post(new EventBusOb("restart", Collections.emptyList()));
        }
        App.isCanDetection = true;
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
        }
         Log.e(TAG,  "目标检测集合为空，继续截取屏幕截图");
    }
}
